package com.program.facesigninsystem.controller;


import com.program.facesigninsystem.api.CommonPage;
import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.iteration.dto.LowerApplyRecord;
import com.program.facesigninsystem.iteration.dto.OwnApplyHistory;
import com.program.facesigninsystem.iteration.dto.RecordDetail;
import com.program.facesigninsystem.iteration.entity.AdminGroupEntity;
import com.program.facesigninsystem.service.AuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/***
 * @author GA
 */
@Api(description = "权限模块")
@RestController
@RequestMapping(value = "/authority")
public class AuthorityController {

    @Resource
    AuthorityService authorityService;

    @ApiOperation("获取某用户的权限")
    @GetMapping("/getOwnAuth")
    public ResponseModel getOwnAuth(@RequestParam String uid) {

        return new ResponseModel(200, "操作成功", authorityService.getOwnAuth(uid));
    }

    @ApiOperation("获取组织列表")
    @GetMapping("/getGroupList")
    public ResponseModel getGroupList(@RequestParam int groupType,
                                      @RequestParam int groupId) {

        List<AdminGroupEntity> groupList = authorityService.getGroupList(groupType, groupId);
        if (groupList != null)
            return new ResponseModel(200, "操作成功", groupList);
        else
            return new ResponseModel(500, "无数据");
    }
    @ApiOperation("获取组织列表1")
    @GetMapping("/getGroupList1")
    public ResponseModel getGroupList1(@RequestParam int groupType) {

        List<AdminGroupEntity> groupList = authorityService.getGroupList1(groupType);
        if (groupList != null)
            return new ResponseModel(200, "操作成功", groupList);
        else
            return new ResponseModel(500, "无数据");
    }
    @ApiOperation("获取该组织列表的上级列表")
    @GetMapping("/getGroupfather")
    public ResponseModel getGroupfather(@RequestParam int groupid) {

        List<String> list=authorityService.getsuperiors(groupid);
        if (list != null)
            return new ResponseModel(200, "操作成功", list);
        else
            return new ResponseModel(500, "无数据");
    }


    @ApiOperation("普通用户申请权限")
    @PostMapping("/subAuthApply")
    public ResponseModel applyAuth(@RequestParam String uid, @RequestParam int groupId, @RequestParam String groupName) {

        if (authorityService.subAuthApply(uid, groupId, groupName))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "无权限");
    }

    @ApiOperation("普通用户申请权限(无上级)")
    @PostMapping("/subAuthApply1")
    public ResponseModel applyAuth1(@RequestParam String uid, @RequestParam int groupLevel, @RequestParam String groupName) {

        if (authorityService.subAuthApply1(uid, groupLevel, groupName))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "无权限");
    }
    @ApiOperation("普通用户获取自己的权限申请记录")
    @GetMapping("/getAuthApplyHistory")
    public ResponseModel getAuthApplyHistory(@RequestParam String uid,
                                             @RequestParam int pageNum,
                                             @RequestParam int pageSize) {

        CommonPage<OwnApplyHistory> ownApplyHistoryCommonPage = authorityService.getAuthApplyHistory(uid, pageNum, pageSize);
        if (ownApplyHistoryCommonPage != null)
            return new ResponseModel(200, "操作成功", ownApplyHistoryCommonPage);
        else
            return new ResponseModel(500, "无数据");
    }

    @ApiOperation("普通用户获取某条权限申请记录明细")
    @GetMapping("/getOneAuthApplyDetail")
    public ResponseModel getOneAuthApplyDetail(@RequestParam int recordId) {

        RecordDetail oneAuthApplyDetail = authorityService.getOneAuthApplyDetail(recordId);
        if (oneAuthApplyDetail != null)
            return new ResponseModel(200, "操作成功", oneAuthApplyDetail);
        else
            return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("组织管理员获取普通用户权限申请记录")
    @GetMapping("/getAuthApplyRecord")
    public ResponseModel getAuthApplyRecord(@RequestParam String uid,
                                            @RequestParam int pageNum,
                                            @RequestParam int pageSize) {

        List<LowerApplyRecord> applyAuthRecordList = authorityService.getAuthApplyRecord(uid, pageNum, pageSize);
        if (applyAuthRecordList != null)
            return new ResponseModel(200, "操作成功", CommonPage.restPage(applyAuthRecordList));
        else
            return new ResponseModel(500, "无数据");
    }
    @ApiOperation("获取绑定的下级")
    @GetMapping("/getBind")
    public ResponseModel getBind(@RequestParam String uid,
                                            @RequestParam int pageNum,
                                            @RequestParam int pageSize) {

        List<LowerApplyRecord> applyAuthRecordList = authorityService.getBind(uid, pageNum, pageSize);
        if (applyAuthRecordList != null)
            return new ResponseModel(200, "操作成功", CommonPage.restPage(applyAuthRecordList));
        else
            return new ResponseModel(500, "无数据");
    }
    @ApiOperation("组织管理员获取自己的申请绑定上级组织的历史记录")
    @GetMapping("/getBindApplyHistory")
    public ResponseModel getBindApplyHistory(@RequestParam String uid,
                                             @RequestParam int pageNum,
                                             @RequestParam int pageSize) {

        CommonPage<OwnApplyHistory> ownApplyHistoryCommonPage = authorityService.getBindApplyHistory(uid, pageNum, pageSize);
        if (ownApplyHistoryCommonPage != null)
            return new ResponseModel(200, "操作成功", ownApplyHistoryCommonPage);
        return new ResponseModel(500, "无绑定记录");
    }

    @ApiOperation("组织管理员向上级发出绑定申请")
    @PostMapping("/subBindApply")
    public ResponseModel subBindApply(@RequestParam String uid,
                                      @RequestParam int groupId) {

        if (authorityService.subBindApply(uid, groupId))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("组织管理员获取下级绑定记录")
    @GetMapping("/getBindApplyRecord")
    public ResponseModel getBindApplyRecord(@RequestParam int groupId,
                                            @RequestParam int pageNum,
                                            @RequestParam int pageSize) {

        List<LowerApplyRecord> lowerApplyRecordList = authorityService.getBindApplyRecord(groupId, pageNum, pageSize);
        if (lowerApplyRecordList != null)
            return new ResponseModel(200, "操作成功", CommonPage.restPage(lowerApplyRecordList));
        else
            return new ResponseModel(500, "无绑定记录");

    }



    @ApiOperation("组织管理员处理权限申请/绑定")
    @PostMapping("/handleApply")
    public ResponseModel handleApply(@RequestParam int applyId,
                                     @RequestParam int applyType,
                                     @RequestParam int handleType,
                                     @RequestParam String rejectReason) {

        if (authorityService.handleApply(applyId, applyType, handleType, rejectReason))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "操作失败");
    }


    @ApiOperation("组织管理员取消下级绑定")
    @PostMapping("/cancelBind")
    public ResponseModel cancelBind(@RequestParam String uid, @RequestParam int groupId) {

        if (authorityService.cancelBind(uid, groupId))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "操作失败");
    }


    @ApiOperation("转换是否开放考勤状态")
    @PostMapping("/changeAttendanceStatus")
    public ResponseModel changeAttendanceStatus(@RequestParam int groupId,
                                                @RequestParam int whetherSeeAttendance) {

        if (authorityService.changeAttendanceStatus(groupId, whetherSeeAttendance))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "操作失败");
    }

    @ApiOperation("绑定教职工")
    @PostMapping("/bindTeacher")
    public ResponseModel bindTeacher(@RequestParam String uid,
                                     @RequestParam String university,
                                     @RequestParam String academy,
                                     @RequestParam String major,
                                     @RequestParam String job_number,
                                     @RequestParam int whetherSeeAttendance) {

        if (authorityService.bindTeacher(uid, university, academy, major, job_number, whetherSeeAttendance))
            return new ResponseModel(200, "操作成功");
        else
            return new ResponseModel(500, "操作失败");
    }

}
